/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
  let l = 1, r = x, ans = 0
  while (l <= r) {
    let mid = Math.floor((l + r) / 2)
    if (mid * mid > x) {
      r = mid - 1
    } else if (mid * mid < x) {
      ans = mid
      l = mid + 1
    } else {
      return mid
    }
  }
  return ans
}
console.log(mySqrt(100))
console.log(mySqrt(0))
console.log(mySqrt(1))
console.log(mySqrt(9))